package com.ztools.pc.service.impl.information;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.export.TeacherExport;
import com.ztools.common.model.pc.information.Teacher;
import com.ztools.common.model.pc.information.TeacherClasses;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.information.TeacherMapper;
import com.ztools.pc.service.information.TeacherClassesService;
import com.ztools.pc.service.information.TeacherService;
import com.ztools.pc.service.sys.DictService;
import com.ztools.pc.util.ExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:一口香菜
 * @Date: 2023/3/13  14:18
 * @Version 1.0
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private DictService dictService;
    @Resource
    private TeacherClassesService teacherClassesService;
    @Resource
    private ExcelUtil excelUtil;

    @Override
    public Page<Teacher> findList(PageUtil<Teacher> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>(pageUtil.getParams());
        Page<Teacher> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<Teacher> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<Teacher> findByObject(Teacher teacher) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>(teacher);
        List<Teacher> list = list(queryWrapper);
        dictionary(list);
        return list;
    }

    @Override
    public List<Teacher> findListByClassesId(Long classesId) {
        QueryWrapper<TeacherClasses> teacherClassesQueryWrapper = new QueryWrapper<>();
        teacherClassesQueryWrapper.in("classes_id", classesId);
        List<TeacherClasses> teacherClassesList = teacherClassesService.list(teacherClassesQueryWrapper);
        if (teacherClassesList.size() > 0) {
            List<Long> teacherIdList = teacherClassesList.stream().map(TeacherClasses::getTeacherId).collect(Collectors.toList());
            List<Teacher> teacherList = listByIds(teacherIdList);
            dictionary(teacherList);
            return teacherList;
        }
        return null;
    }

    @Override
    public boolean add(Teacher teacher) {
        return save(teacher);
    }

    @Override
    public boolean update(Teacher teacher) {
        Teacher byId = getById(teacher.getId());
        return updateById(teacher);
    }

    @Override
    public boolean delete(Teacher teacher) {
        boolean b = false;
        Teacher byId = getById(teacher);
        b = removeById(teacher);
        return b;
    }

    @Override
    public void exportExcel(HttpServletResponse response, Teacher teacher) {
        long count = count();
        long sheetNumber = ExcelUtil.SHEET_NUMBER;
        long pageSize = excelUtil.getPageSize(sheetNumber, count);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>(teacher);
        for (int i = 0; i < pageSize; i++) {
            Page<Teacher> pages = new Page<>(i, sheetNumber);
            Page<Teacher> page = page(pages, queryWrapper);
            List<Teacher> list = page.getRecords();
            dictionary(list);
            excelUtil.exportExcel(response, i, list, TeacherExport.class, "教师基本信息");
        }
    }


    /**
     * 字典翻译
     *
     * @param teacherList 对象集合
     */
    public void dictionary(List<Teacher> teacherList) {
        for (Teacher teacher : teacherList) {
            dictionary(teacher);
        }
    }

    public void dictionary(Teacher teacher) {
        if (teacher != null) {
            // 性别翻译
            if (teacher.getSex() != null) {
                teacher.setSexName(dictService.findByTypeCode(DictUtil.SEX, teacher.getSex() + ""));
            }
            // 职位翻译
            if (teacher.getPosition() != null) {
                teacher.setPositionName(dictService.findByTypeCode(DictUtil.POSITION, teacher.getPosition() + ""));
            }
            // 职位级别翻译
            if (teacher.getPositionRank() != null) {
                teacher.setPositionRankName(dictService.findByTypeCode(DictUtil.POSITION_RANK, teacher.getPositionRank() + ""));
            }
            // 社会面貌翻译
            if (teacher.getSocialPresence() != null) {
                teacher.setSocialPresenceName(dictService.findByTypeCode(DictUtil.SOCIAL_PRESENCE, teacher.getSocialPresence() + ""));
            }
            // 证件翻译
            if (teacher.getDocumentType() != null) {
                teacher.setDocumentTypeName(dictService.findByTypeCode(DictUtil.DOCUMENT_TYPE, teacher.getDocumentType() + ""));
            }
            // 民族翻译
            if (teacher.getNation() != null) {
                teacher.setNationName(dictService.findByTypeCode(DictUtil.NATION, teacher.getNation() + ""));
            }
        }
    }

}
